package com.cheng.example.book.advise151.chap3.client45;

import java.util.ArrayList;
import java.util.List;


/**
 * 建议45：覆写equals方法时不要识别不出自己
 *
 * 自反性原则：对于任何非空引用x，x.equals(x)应该返回true
 */
public class Person {
    private String name;

    public Person(String _name) {
        name = _name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Person) {
            Person p = (Person) obj;
            return name.equalsIgnoreCase(p.getName());
//            return name.equalsIgnoreCase(p.getName().trim());
        }
        return false;
    }

    public static void main(String[] args) {
        Person p1 = new Person("张三");
        Person p2 = new Person("张三  ");
        List<Person> list = new ArrayList<Person>();
        list.add(p1);
        list.add(p2);
        System.out.println("列表中是否包含张三：" + list.contains(p1));
        System.out.println("列表中是否包含张三：" + list.contains(p2));
    }

}